$('.select2').select2();
// $.get("header_menu.html",function(data){ $("#header").html(data); });
$.get("footer.html",function(data){ $("#footer").html(data); });

$(document).ready(function () {
    // $("#order").addClass("active");
    // $("#order1").addClass("active");

    getData1(1);
  $('.search_btn').click(function(){
    console.log($('#order_status').val());
    getData1(1);
  });

var orderN='';
var orderM='';
function getData1(curr){
    // var status = $('#order_status').val();
    // var product = $('.name_text').val();
    // var phone = $('.num_text').val();
    // console.log(status,product,phone);
    $.ajax({
        type:"get",
        url:"http://114.116.72.220:9999/GetOrderInfo",
        async:true,
        data:{
            page:curr ,
            row: 5,
            product:$('.name_text').val(),
            phone:$('.num_text').val(),
            status:$('#order_status').val()
        },
        success:function(data){
            console.log(data);
            if(data[0].length==0){
                layer.msg('暂无数据')
            }
            TableView(data[0]);
            pageDivd(curr,data[1]);
        }
    });
}

function pageDivd(curr,page_num){
    layui.use(['laypage', 'layer'], function(){
        var laypage = layui.laypage
            ,layer = layui.layer;
        laypage.render({
            elem: 'demo7'
            ,count: page_num
            ,limit:5
            ,curr:curr
            ,jump: function(obj,first){
                if(!first){
                    getData1(obj.curr);

                }
            }
        });
    })
}

function TableView(data){
    $('.table_data').html('');

    for (var i=0;i<data.length;i++) {
        var orderStatus = data[i].status;
        var className = '';
        var sendClass = 'btn2';
        var disabledFlag = 'disabled';

        if(orderStatus=='待上门'){
            className = 'table_text1';
            sendClass = 'btn1';
            disabledFlag = '';
        }else if(orderStatus=='待收货'){
            className = 'table_text3';
        }else {
            className = 'table_text2';
        }

        var trText = `<tr num="${data[i].order_id}" class="${i%2 ? '' : 'tr_back'}">
                                <td>${i+1}</td>
		      		    		<td>${data[i].order_id}</td>
		      		    		<td>${data[i].product_name}</td>
		      		    		<td>${data[i].number}</td>
		      		    		<td>
		      		    			${data[i].time}
		      		    		</td>
		      		    		<td>
		      		    			${data[i].receive}
		      		    		</td>
		      		    		<td>
		      		    			${data[i].phone}
		      		    		</td>
		      		    		<td class="${className}">
		      		    			${data[i].status}
		      		    		</td>
		      		    		<td style="width: 252px">
		      		    			<button class="order_send ${sendClass}" ${disabledFlag}>订单发货</button>
		      		    			<button class="order_detail btn1" style="margin-left: 16px">订单详情</button>
		      		    		</td>
		      		    	</tr>
		      		    	`;

        $('.table_data').append(trText) ;

    }
    look_detail();
}
function look_detail(){
    $('.order_detail').click(function(){
        var id = $(this).parent().parent().attr('num');
        window.location.href="http://114.116.72.220:9010/src/order_detail.html?orderId="+id;
    });

    var order_id;
    //订单发货
    $('.order_send').click(function(){
        $(".send_num").val("");
        $('.order_form').css('display','block');
        $('.close_btn').click(function(){
            $('.order_form').css('display','none');
        })
        order_id = $(this).parent().parent().attr('num');
    });
    //提交
    $('.order_save').click(function(){
        $.ajax({
            type:"get",
            url:"http://114.116.72.220:9999/OrderDelivered",
            async:true,
            data:{
                orderId:order_id,
                company:$('.send_text').val(),
                no:$('.send_num').val(),
            },
            success:function(data){
                if(data=='success'){
                    layer.msg('提交成功', {time: 500}, function () {
                        location.reload();
                    });
                    // $('.order_form').css('display','none');
                }else{
                    layer.msg('提交失败')
                }
            }
        });
    })

    //关闭订单
    $('.order_btn').click(function(){
        var index = $(this).attr('index');
        var orderNum = $(this).parents('tr').attr('num');

        orderN =  $(this).attr('index');
        orderM = $(this).parents('tr').attr('num');
        if(index!=1){
            $.ajax({
                type:"get",
                url:"http://114.116.72.220:9000/",
                async:true,
                data:{
                    Id:orderNum,
                    Kind:index,
                    Company:$('.send_text').val(),
                    No:$('.send_num').val(),
                    UserId:$.cookie('UserId')
                },
                success:function(data){
                    if(data=='success'){
                        layer.msg('提交成功')
                    }else{
                        layer.msg('提交失败')
                    }
                }
            });
        }



    })

}

});